package com.cloudsigns.factorymanager2.engineering.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;

import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.IView.IReceiveOrBackPublicationOrderView;
import com.cloudsigns.factorymanager2.engineering.activity.PublicationFeedBackInfoActivity;
import com.cloudsigns.factorymanager2.engineering.adapter.AddPublicationOrderAdapter;

import com.cloudsigns.factorymanager2.engineering.adapter.GridAdapter;
import com.cloudsigns.factorymanager2.engineering.adapter.MediaForPublicationAdapter;
import com.cloudsigns.factorymanager2.engineering.bean.MediaBean;

import com.cloudsigns.factorymanager2.engineering.bean.PublicationOrderDetailBean;

import com.cloudsigns.factorymanager2.engineering.presenter.ReceiveOrBackPublicationOrderPresenter;
import com.cloudsigns.factorymanager2.event.EngineeringOrderDetailForTeamEvent;
import com.cloudsigns.factorymanager2.project.myinerface.ItemClickListener;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.DateUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import de.greenrobot.event.EventBus;

/**
 * 工单信息
 * Created by tony on 2018/5/26.
 */

public class WorkOrderInfomationForPublicationFragment extends Fragment implements View.OnClickListener, ItemClickListener, IReceiveOrBackPublicationOrderView {
    private TextView tv_project_name, tv_engineering_name;
    private View rl_project;
    private View ll_engineering_info_content;
    private ImageView img_project_select;
    private TextView edt_pubication_address,edt_pubication_cycle_time, edt_dispath_order_person_colon, edt_dispath_order_time_colon;
    private GridView gridView;
    private GridAdapter gridAdapter;
    private List<String> picUrlList = new ArrayList<>();
    private ScrollView scrollview;
    private LinearLayout ll_region;
    private TextView btn_receive_order, btn_back_order;
    private boolean isClick;
    private final String TAG = WorkOrderInfomationForPublicationFragment.class.getSimpleName();
    private LayoutInflater layoutInflater;
    private PublicationOrderDetailBean publicationOrderDetailBean;
    Map<String, List<MediaBean>> mediaBeanMap = new HashMap<>();
    private CommonEnum.STATUS status;
    private ReceiveOrBackPublicationOrderPresenter receiveOrBackPublicationOrderPresenter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        receiveOrBackPublicationOrderPresenter = new ReceiveOrBackPublicationOrderPresenter(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_workorderinfomation_for_publication, null);
        tv_project_name = view.findViewById(R.id.tv_project_name);
        tv_engineering_name = view.findViewById(R.id.tv_engineering_name);
        edt_pubication_address=view.findViewById(R.id.edt_pubication_address);
        edt_pubication_cycle_time = view.findViewById(R.id.edt_pubication_cycle_time);
        edt_dispath_order_person_colon = view.findViewById(R.id.edt_dispath_order_person_colon);
        edt_dispath_order_time_colon = view.findViewById(R.id.edt_dispath_order_time_colon);
        ll_engineering_info_content = view.findViewById(R.id.ll_engineering_info_content);
        img_project_select = view.findViewById(R.id.img_project_select);
        scrollview = view.findViewById(R.id.scrollview);
        ll_region = view.findViewById(R.id.ll_region);
        rl_project = view.findViewById(R.id.rl_project);
        btn_receive_order = view.findViewById(R.id.btn_receive_order);
        btn_back_order = view.findViewById(R.id.btn_back_order);

        gridView = view.findViewById(R.id.gridView);
        gridView.setNumColumns(3);

        gridAdapter = new GridAdapter(getActivity());
        gridAdapter.setData(picUrlList);
        gridView.setAdapter(gridAdapter);

        img_project_select.setOnClickListener(this);
        btn_receive_order.setOnClickListener(this);
        btn_back_order.setOnClickListener(this);

        layoutInflater = getActivity().getLayoutInflater();

        img_project_select.setBackgroundResource(R.mipmap.arrow_down_icon);
        return view;
    }

    private void showEngineeringInfoDialog() {
        if (ll_engineering_info_content.getVisibility() == View.VISIBLE) {
            ll_engineering_info_content.setVisibility(View.GONE);
            img_project_select.setBackgroundResource(R.mipmap.arrow_down_icon);
        } else {
            ll_engineering_info_content.setVisibility(View.VISIBLE);
            img_project_select.setBackgroundResource(R.mipmap.arrow_top);
        }
    }

    public void initData(PublicationOrderDetailBean publicationOrderDetailBean) {
        if (null != publicationOrderDetailBean) {
            mediaBeanMap.clear();

            this.publicationOrderDetailBean = publicationOrderDetailBean;

            tv_project_name.setText(publicationOrderDetailBean.getRegionName());
            tv_engineering_name.setText("/" + publicationOrderDetailBean.getPublicationContent());
            edt_pubication_cycle_time.setText(DateUtil.getYyyy_Mm_Dd(publicationOrderDetailBean.getBeginDate()) + DateUtil.getYyyy_Mm_Dd(publicationOrderDetailBean.getFinishDate()));
            edt_dispath_order_person_colon.setText(publicationOrderDetailBean.getReceiveAccountId());
            edt_dispath_order_time_colon.setText(DateUtil.getYyyy_Mm_Dd(publicationOrderDetailBean.getSendOrderTime()));

            if (publicationOrderDetailBean.getAssignStatus() == CommonEnum.AssignStatus.CREATED
                    || publicationOrderDetailBean.getAssignStatus() == CommonEnum.AssignStatus.EDITED
                    || publicationOrderDetailBean.getAssignStatus() == CommonEnum.AssignStatus.SEND) {
                isClick = true;
            } else if (publicationOrderDetailBean.getAssignStatus() == CommonEnum.AssignStatus.RECEIVED
                    || publicationOrderDetailBean.getAssignStatus() == CommonEnum.AssignStatus.RETURNED
                    || publicationOrderDetailBean.getAssignStatus() == CommonEnum.AssignStatus.COMPLETED) {
                isClick = false;
            }

            setButton();

            List<String> picList = publicationOrderDetailBean.getPicList();
            if (null != picList && picList.size() > 0) {
                picUrlList.clear();
                picUrlList.addAll(picList);
                gridAdapter.setData(picUrlList);
                gridAdapter.notifyDataSetChanged();
            }


            List<MediaBean> mediaBeanList = publicationOrderDetailBean.getMediaList();
            if (null != mediaBeanList) {
                for (MediaBean mediaBean : mediaBeanList) {
                    String areaName = mediaBean.getAreaName();
                    if (mediaBeanMap.containsKey(areaName)) {
                        mediaBeanMap.get(areaName).add(mediaBean);
                    } else {
                        List<MediaBean> tmpMediaBeanList = new ArrayList<>();
                        tmpMediaBeanList.add(mediaBean);
                        mediaBeanMap.put(areaName, tmpMediaBeanList);
                    }
                }
            }

            updateUi();
        }
    }

    private void updateUi() {
        if (null != mediaBeanMap && null != ll_region) {
            ll_region.removeAllViews();

            for (Map.Entry<String, List<MediaBean>> entry : mediaBeanMap.entrySet()) {
                View view = layoutInflater.inflate(R.layout.layout_point_location_content, null);
                TextView tv_area_name = view.findViewById(R.id.tv_area_name);
                tv_area_name.setText(entry.getKey());

                ListView lv_media = view.findViewById(R.id.lv_media);

                List<MediaBean> mediaBeanList = entry.getValue();
                MediaForPublicationAdapter adapter = new MediaForPublicationAdapter(getActivity(), TAG);
                adapter.setData(mediaBeanList);
//                adapter.setItemClickListener(this);
                lv_media.setAdapter(adapter);
                ll_region.addView(view);
            }
        }
        scrollview.smoothScrollTo(0, 0);
    }


    private void setButton() {
        if (isClick) {
            btn_receive_order.setBackgroundResource(R.color.rgb_00B050);
            btn_back_order.setBackgroundResource(R.color.rgb_C00000);
        } else {
            btn_receive_order.setBackgroundResource(R.color.rgb_D9D9D9);
            btn_back_order.setBackgroundResource(R.color.rgb_D9D9D9);
        }
    }

    private void receiveOrder() {
        status = CommonEnum.STATUS.RECEIVED;
        isClick = false;
        setButton();
        receiveOrBackPublicationOrderPresenter.receiveOrBackPublicationOrder();
    }

    private void backOrder() {
        status = CommonEnum.STATUS.RETURNED;
        isClick = false;
        setButton();
        receiveOrBackPublicationOrderPresenter.receiveOrBackPublicationOrder();
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            // 项目名称/工程名称 收放
            case R.id.img_project_select:
                showEngineeringInfoDialog();
                break;
                // 接收工单
            case R.id.btn_receive_order:
                if (isClick)
                    receiveOrder();
                break;
                // 退回工单
            case R.id.btn_back_order:
                if (isClick)
                    backOrder();
                break;


        }
    }


    @Override
    public void itemClick(View view, int position) {
        Intent mIntent;
        MediaBean mediaBean = (MediaBean) view.getTag();
        if (null != mediaBean) {
            mIntent = new Intent(getActivity(), PublicationFeedBackInfoActivity.class);
            mIntent.putExtra(ConstantUtil.KEY_1, publicationOrderDetailBean);
            mIntent.putExtra(ConstantUtil.KEY_2, mediaBean);
            startActivity(mIntent);
//            if (mediaBean.getStatus() == CommonEnum.MediaStatus.PROCESSING) {
//                mIntent = new Intent(getActivity(), PublicationFeedBackInfoActivity.class);
//                mIntent.putExtra(ConstantUtil.KEY_1, publicationOrderDetailBean);
//                mIntent.putExtra(ConstantUtil.KEY_2, mediaBean);
//                startActivity(mIntent);
//            } else if (mediaBean.getStatus() == CommonEnum.MediaStatus.PAUSE) {
//
//            } else if (mediaBean.getStatus() == CommonEnum.MediaStatus.PROBLEM_APPEAR) {
//
//            } else if (mediaBean.getStatus() == CommonEnum.MediaStatus.PROBLEM_FEEDBACK) {
//                ;
//            } else if (mediaBean.getStatus() == CommonEnum.MediaStatus.COMPLETE_FEEDBACK) {
//
//            } else if (mediaBean.getStatus() == CommonEnum.MediaStatus.COMPLETE) {
//
//            } else {
//                mIntent = new Intent(getActivity(), PublicationFeedBackInfoActivity.class);
//                mIntent.putExtra(ConstantUtil.KEY_1, publicationOrderDetailBean);
//                mIntent.putExtra(ConstantUtil.KEY_2, mediaBean);
//                startActivity(mIntent);
//            }


        }
    }


    @Override
    public String getOrderId() {
        if (null != publicationOrderDetailBean)
            return publicationOrderDetailBean.getId();
        return "";
    }

    @Override
    public CommonEnum.STATUS getStatus() {
        return status;
    }

    @Override
    public void receiveOrBackOrderSuccessed() {
        if (null != status) {
            if (status == CommonEnum.STATUS.RECEIVED)
                Toast.makeText(getActivity(), "接收成功！", Toast.LENGTH_SHORT).show();
            else if (status == CommonEnum.STATUS.RETURNED)
                Toast.makeText(getActivity(), "退回成功！", Toast.LENGTH_SHORT).show();
        }
        EventBus.getDefault().post(new EngineeringOrderDetailForTeamEvent());
        isClick = false;
        setButton();
    }

    @Override
    public void receiveOrBackOrderFailed(String desc) {
        Toast.makeText(getActivity(), desc, Toast.LENGTH_SHORT).show();
        isClick = false;
        setButton();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

}
